package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lihy
 * @date 2023/7/28 23:41
 */
public class HttpsClientUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpsClientUtil.class);
    private static final int socketTimeout = 3000;
    private static final int connectTimeout = 3000;
    private static final int connectionRequestTimeout = 3000;
    private static RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .setSocketTimeout(socketTimeout) // 服务端相应超时
            .setConnectTimeout(connectTimeout) // 建立socket链接超时时间
            .build();

    public static String doGet(String url) {
        if (StringUtils.isEmpty(url)) {
            return "";
        }
        return doGet(url, new HashMap<>());
    }

    public static String doGet(String url, Map<String, String> param) {
        return doGet(url, param, false);
    }

    public static String doGet(String urlStr, Map<String, String> param, boolean useProxy) {
        if (StringUtils.isEmpty(urlStr)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        HttpURLConnection connection = null;
        BufferedReader reader = null;
        try {
            log.info("请求地址：{}",urlStr);
            URL url = new URL(urlStr);
            connection = (HttpURLConnection)url.openConnection();
            // 设置请求方式
            connection.setRequestMethod("GET");
            connection.connect();
            // 获取响应码
            int responseCode = connection.getResponseCode();
            String line = "";
            if (responseCode == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    // 读取到的内容给line变量
                    log.info("读取内容："+line);
                    sb.append(line);
                }
            }
        } catch (Exception e) {
            log.error("https请求异常",e);
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error("流关闭异常",e);
                }
            }
        }
        return sb.toString();
    }
}
